package cn.wit.sst.components;

import java.util.Arrays;
import java.util.List;

import org.controlsfx.control.spreadsheet.GridBase;
import org.controlsfx.control.spreadsheet.SpreadsheetCell;
import org.controlsfx.control.spreadsheet.SpreadsheetCellType;
import org.controlsfx.control.spreadsheet.SpreadsheetView;

import cn.wit.sst.dto.ScoreRankDto;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Node;

public class SpreadsheetHelper {
	private static final double GRID_ROW_HEIGHT = 30D;

	/**
	 * 创建分数统计表格
	 *
	 * @param scores - 分数集合
	 * @return
	 */
	public static Node createSheet(List<ScoreRankDto> scores) {
		SpreadsheetView spreadsheet = new SpreadsheetView();

		GridBase grid = new GridBase(scores.size() + 1, 4);
		// 设置表格行高
		grid.setRowHeightCallback(row -> GRID_ROW_HEIGHT);

		buildGrid(grid, scores);

		spreadsheet.setGrid(grid);
		spreadsheet.getFixedRows().add(0); // 表头
		spreadsheet.getColumns().get(0).setFixed(true); // 固定第一列
		spreadsheet.setShowColumnHeader(false); // 不显示列号
		spreadsheet.setShowRowHeader(false); // 不显示行号
		spreadsheet.getColumns().get(0).setPrefWidth(142);
		spreadsheet.getColumns().get(1).setPrefWidth(250);
		spreadsheet.setPrefHeight(GRID_ROW_HEIGHT * (scores.size() + 1.5));
		// spreadsheet.getStylesheets().add(Utils.class.getResource("spreadsheetSample.css").toExternalForm());

		return spreadsheet;
	}

	private static void buildGrid(GridBase grid, List<ScoreRankDto> scores) {
		ObservableList<ObservableList<SpreadsheetCell>> rows = FXCollections.observableArrayList();

		rows.add(getHeaders(grid, 0));
		rows.addAll(getDataRows(grid, 1, scores));
		grid.setRows(rows);

		int spanStart = 0;
		for (int i = 0; i < scores.size(); i++) {
			ScoreRankDto score = scores.get(i);
			if (score.getDisplaySemester() == null) {	// 当前行为总分行，合并
				grid.spanRow(
						i - spanStart + 1, 		// 合并行数
						spanStart + 1, 			// 从第几行合并，+1是因为有标题行
						0
				);

				spanStart = i + 1;	// 下一个合并行
			}
		}

		// grid.getRows().get(15).get(1).getStyleClass().add("span");
	}

	private static final List<String> HEADERS = Arrays.asList("学期", "科目", "分数", "排名");

	private static ObservableList<SpreadsheetCell> getHeaders(GridBase grid, int row) {
		final ObservableList<SpreadsheetCell> headers = FXCollections.observableArrayList();

		for (int column = 0; column < grid.getColumnCount(); ++column) {
			SpreadsheetCell cell = SpreadsheetCellType.STRING.createCell(row, column, 1, // rowspan
					1, // colspan
					HEADERS.get(column) // value
			);
			cell.setEditable(false);
			cell.getStyleClass().add("header");
			headers.add(cell);
		}

		return headers;
	}

	private static ObservableList<ObservableList<SpreadsheetCell>> getDataRows(
			GridBase grid, int row, List<ScoreRankDto> scores) {

		ObservableList<ObservableList<SpreadsheetCell>> dataRows = FXCollections.observableArrayList();

		int rowIndex = row;
		for (ScoreRankDto score : scores) {
			ObservableList<SpreadsheetCell> currentRow = FXCollections.observableArrayList();

			currentRow.add(createCell(rowIndex, 0, score.getDisplaySemester()));
			currentRow.add(createCell(rowIndex, 1, score.getCourse()));
			currentRow.add(createCell(rowIndex, 2, score.getScore() + ""));
			currentRow.add(createCell(rowIndex, 3, score.getRank() + ""));

			rowIndex++;

			dataRows.add(currentRow);
		}

		return dataRows;
	}

	private static SpreadsheetCell createCell(int row, int column, String value) {
		SpreadsheetCell cell = SpreadsheetCellType.STRING.createCell(row, column, 1 /* rowspan */,
				1 /* column span */, value);
		cell.setEditable(false);

		return cell;
	}
}
